﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
	using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.AP.APBill;
    using UFIDA.U9.AR.ARBill;
    using UFIDA.U9.Base;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Util.Context;

	/// <summary>
	/// ARBillApprove partial 
	/// </summary>	
	public partial class ARBillApprove 
	{	
		internal BaseStrategy Select()
		{
			return new ARBillApproveImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class ARBillApproveImpementStrategy : BaseStrategy
	{
		public ARBillApproveImpementStrategy() { }

		public override object Do(object obj)
		{						
			ARBillApprove bpObj = (ARBillApprove)obj;

			try
			{
				if (bpObj.Data == null || bpObj.Data.Count == 0)
					throw new Exception("传入参数为空");
				List<DocDTOData> retlist = new List<DocDTOData>();

				//List<object> inputdtos = DTOUtils.DTOListPropCopy(bpObj.Data.ConvertAll(item => item as object)
				//	, typeof(CompRptKeyDTOData));
				foreach (var dto in bpObj.Data)
				{
					ARBillHead entity = ARBillHead.Finder.Find($"DocNo='{dto.DocNo}'");
					if (entity == null)
					{
						//新增逻辑
						throw new Exception($"单号:{dto.DocNo}不存在!");

					}
					else
					{
						if (entity.DocStatus == UFIDA.U9.CBO.FI.Enums.BillStatusEnum.Approved)
							throw new Exception($"单号:{entity.DocNo}已经审核!");

						ARBillBase.ApproveDoc(entity);

						DocDTOData retdto = new DocDTOData()
						{
							DocNo = entity.DocNo,
							ID = entity.ID.ToString(),
						};

						retlist.Add(retdto);
					}
				}
				return ResultUtils.success(bpObj,bpObj.Data,retlist);
			}
			catch (Exception ex)
			{
				return ResultUtils.error(bpObj,bpObj.Data,ex);
			}
		}

        
    }

	#endregion
	
	
}